import React, { Component } from 'react'
import {
    View,
    Text,
    Button,
    ActivityIndicator,
    FlatList
} from 'react-native'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { loadMore } from '../../actions/PurchaseAction';
import Style from '../../utils/style/style'
import WXStyle from '../../utils/style/wxstyle';
import * as fetch from '../../utils/fetch'
import RfidModule from '../../components/Rfid';

import Loading from '../../views/loading';
import SearchInput from '../../components/input';
import PurchaseItem from '../../views/purchaseItem';
import DataList from '../../views/DataList';
import PageLoading from '../../components/pageLoding'

class Purchase extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            keyword: '',
            dataList: [],
            pageNum: 0,
            hasMore: true,
            pageSize: 10,
        }
    }
    static navigationOptions = {
        title: '采购',
    };
    componentDidMount() {
        this._getData()
        // this._fetchData('', '');
    }
    componentWillUnmount() {
        RfidModule.close()
    }

    search = (text) => {
        this._getData(text)
    }

    _getData = (keyword) => {
        const api = "erp_purchases";//如果把分页组件化 this.props.api
        let { pageNum, pageSize, hasMore } = this.props
        pageNum = pageNum + 1;
        if (hasMore) {
            this.setState({
                pageNum: pageNum
            })
            let params = {
                pageNum: pageNum,
                pageSize: pageSize,
                expand:'purchase_supplier($select=RecId,Name,supplier_id,Phone,AddressStr,AddressSupplement,Arrears),purchase_plist($expand=plist_into),erp_purchase_file($select=Url)',
                orderByDesc:'Code',
            }
            if (keyword) {
                params['keyword'] = keyword
            }
            this.props.loadMore(api, params)
        }
    }
    
    _PageLoading = () => (
        <PageLoading
            hasMore={this.props.hasMore}
            loading={this.props.loading}
        />
    )
    _renderItem = ({ item }) => {
        return (
            <PurchaseItem
                id={item.purchase_id}
                item={item}
                pressItem={() => { this._pressItem(item) }}
            />
        )
    }
    _pressItem(item) {
        const resData = {
            supplier: item.purchase_supplier,
            TotalPrice: item.TotalPrice,
            ActualAmount: item.ActualAmount,
            Arrears: item.Arrears,
            PurchaseTime: item.PurchaseTime,
            Remarks: item.Remarks,
            PurchaseId: item.purchase_id,
            RecId: item.RecId,
            images:item.images
        }
        this.props.navigation.navigate('PurchaseDetail', { item: resData })
    }
    render() {
        const { loading, dataList, hasMore } = this.props;
        return (
            <View style={{ flex: 1 }}>
                {/* <View style={[Style.searchWrapper]}>
                    <View style={[Style.centered]}>
                        <SearchInput search={this.search} />
                    </View>
                </View> */}
                {
                    loading && !dataList ?
                        <Loading /> : !loading && dataList.length == 0 ?
                            <View style={{ flex: 1 }}>
                                <Text>暂无数据</Text>
                            </View> :
                            <DataList
                                data={dataList}
                                renderItem={this._renderItem}
                                LoadMore={this._getData}
                                loading={loading}
                                hadMore={hasMore}
                                footer={this._PageLoading}
                                itemHeight={81}
                            />
                }
            </View>
        );
    }
}
export default connect(
    state => ({
        loading: state.Purchase.loading,
        keyword: state.Purchase.keyword,
        dataList: state.Purchase.dataList,
        pageNum: state.Purchase.pageNum,
        hasMore: state.Purchase.hasMore,
        pageSize: state.Purchase.pageSize,
    }),
    dispatch => ({ loadMore: bindActionCreators(loadMore, dispatch) })
)(Purchase)